package br.com.evoluy.repositorio;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import br.com.evoluy.controle.ControlePaciente;
import br.com.evoluy.entidade.Paciente;
import br.com.evoluy.util.ConnectionFactory;

public class RepositorioPaciente {

    ControlePaciente cp = null;
	
	public void salvaPaciente(Paciente paciente)
	{
        Connection con = new ConnectionFactory().getConnection();
		String sql = "insert into paciente"+"(id,cns,nome,endereco,numero,bairro,cidade,uf,cep,estadocivil,telefone,rg,cpf,"+
				     "datanascimento,email,profissao,sexo,pai,mae,estuda)"+
                     "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		
		try 
		{	
			PreparedStatement stmp = (PreparedStatement) con.prepareStatement(sql);
			stmp.setLong(1, paciente.getId());
			stmp.setString(2, paciente.getCns());
			stmp.setString(3, paciente.getNome());
			stmp.setString(4, paciente.getEndereco());
			stmp.setString(5, paciente.getNumero());
			stmp.setString(6, paciente.getBairro());
			stmp.setString(7, paciente.getCidade());
			stmp.setString(8, paciente.getUf());
			stmp.setString(9, paciente.getCep());
			stmp.setString(10, paciente.getEstadocivil());
			stmp.setString(11, paciente.getTelefone());
			stmp.setString(12, paciente.getRg());
			stmp.setString(13, paciente.getCpf());
			stmp.setString(14, paciente.getDatanascimento());
			stmp.setString(15, paciente.getEmail());
			stmp.setString(16, paciente.getProfissao());
			stmp.setString(17, paciente.getSexo());
			stmp.setString(18, paciente.getPai());
			stmp.setString(19, paciente.getMae());
			stmp.setString(20, paciente.getEstuda());
			stmp.execute();
			stmp.close();
		} 
		catch (SQLException e) 
		{	
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				con.close();
			} 
			catch (SQLException e) 
			{
				e.printStackTrace();
			}
		}
	}
	
	
	public String procuraCNS(String cns)
	{
		String retorno = "";
		String sub = null;
		String sql = "select * from paciente";
		cp = new ControlePaciente();
	
		Connection con = new ConnectionFactory().getConnection();
		
		try 
		{		
			PreparedStatement stmp = (PreparedStatement) con.prepareStatement(sql);
			ResultSet rs = stmp.executeQuery(sql);		
			
			while(rs.next())
			{
			  sub = cp.GeraCNS(rs.getString("cns"));
	
			  if(sub.equals(cns))
			  {
				  retorno = String.valueOf(rs.getString("id"));
				  break;
			  }  
			}
			
			rs.close();
			stmp.close();

		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		finally
		{
			try 
			{	
				con.close();
			} 
			catch (SQLException e) 
			{
				e.printStackTrace();
			}
		}			
	  return retorno; 
	}
	
	
	public boolean procuraCNS2(String cns)
	{
		boolean retorno = false;
		String sub = null;
		String sql = "select * from paciente";
		cp = new ControlePaciente();
	
		Connection con = new ConnectionFactory().getConnection();
		
		try 
		{		
			PreparedStatement stmp = (PreparedStatement) con.prepareStatement(sql);
			ResultSet rs = stmp.executeQuery(sql);		
			
			while(rs.next())
			{
			  sub = cp.GeraCNS(rs.getString("cns"));
	
			  if(sub.equals(cns))
			  {
				  retorno = true;
				  break;
			  }  
			}
			
			rs.close();
			stmp.close();

		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		finally
		{
			try 
			{	
				con.close();
			} 
			catch (SQLException e) 
			{
				e.printStackTrace();
			}
		}			
	  return retorno; 
	}
	
	
	public Paciente procuraPaciente(String id)
	{
		Paciente paciente = null;
        String sql = "select * from paciente where id = '"+id+"'";
		
		Connection con = new ConnectionFactory().getConnection();
		
		try 
		{		
			PreparedStatement stmp = (PreparedStatement) con.prepareStatement(sql);
			ResultSet rs = stmp.executeQuery(sql);
			
			while(rs.next())
			{
				paciente = new Paciente();
				paciente.setId(rs.getInt("id"));
				paciente.setCns(rs.getString("cns"));
				paciente.setNome(rs.getString("nome"));
				paciente.setEndereco(rs.getString("endereco"));
				paciente.setNumero(rs.getString("numero"));
				paciente.setBairro(rs.getString("bairro"));
				paciente.setCidade(rs.getString("cidade"));
				paciente.setUf(rs.getString("uf"));
				paciente.setCep(rs.getString("cep"));
				paciente.setEstadocivil(rs.getString("estadocivil"));
				paciente.setTelefone(rs.getString("telefone"));
				paciente.setRg(rs.getString("rg"));
				paciente.setCpf(rs.getString("cpf"));
				paciente.setDatanascimento(rs.getString("datanascimento"));
				paciente.setEmail(rs.getString("email"));
				paciente.setProfissao(rs.getString("profissao"));
				paciente.setSexo(rs.getString("sexo"));
				paciente.setPai(rs.getString("pai"));
				paciente.setMae(rs.getString("mae"));
				paciente.setEstuda(rs.getString("estuda"));
			    break;
			}
			
			rs.close();
			stmp.close();

		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		finally
		{
			try 
			{	
				con.close();
			} 
			catch (SQLException e) 
			{
				e.printStackTrace();
			}
		}			
		
		return paciente;
	}

}
